package com.easychat.controller;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.entity.constants.Constants;
import com.easychat.entity.dto.TokenUserInfoDto;
import com.easychat.entity.po.UserInfo;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.entity.vo.account.request.LoginReqVO;
import com.easychat.entity.vo.account.request.RegisterReqVO;
import com.easychat.entity.vo.account.response.CheckCodeRspVO;
import com.easychat.entity.vo.account.response.LoginRspVO;
import com.easychat.exception.BusinessException;
import com.easychat.service.UserInfoService;
import com.easychat.utils.RedisUtil;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.ChineseCaptcha;
import com.wf.captcha.GifCaptcha;
import jodd.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author: yudaxian
 * @Description: TODO
 * @DateTime: 2024/8/6 18:41
 **/
@RestController("accountController")
@RequestMapping("/account")
@Validated
@Slf4j
public class AccountController extends ABaseController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private RedisUtil redisUtil;

    @RequestMapping("/checkCode")
    public ResponseVO checkCode() {
        GifCaptcha captcha = new GifCaptcha(100, 42);
        String code = captcha.text();
        log.info("验证码：{}", code);
        String captchaBase64 = captcha.toBase64();
        String captchaKey = UUID.randomUUID().toString();
        log.info("验证码key：{}", Constants.REDIS_KEY_CHECK_CODE + captchaKey);
        redisUtil.set(Constants.REDIS_KEY_CHECK_CODE + captchaKey, code, Constants.REDIS_EXPIRE_TIME_1MIN);

        CheckCodeRspVO checkCodeRspVO = CheckCodeRspVO.builder()
                .checkCode(captchaBase64)
                .checkCodeKey(captchaKey)
                .build();

        return getSuccessResponseVO(checkCodeRspVO);
    }

    @RequestMapping("/register")
    public ResponseVO register(RegisterReqVO registerReqVO) {
        try {
            log.info("注册请求的Redis Key：{}", Constants.REDIS_KEY_CHECK_CODE + registerReqVO.getCheckCodeKey());
            String redisCheckCode = (String) redisUtil.get(Constants.REDIS_KEY_CHECK_CODE + registerReqVO.getCheckCodeKey());
            if (!registerReqVO.getCheckCode().equalsIgnoreCase(redisCheckCode)) {
                throw new BusinessException("验证码不正确");
            }
            userInfoService.register(registerReqVO.getNickName(), registerReqVO.getPassword(), registerReqVO.getEmail());
            return getSuccessResponseVO(null);
        } finally {
            redisUtil.del(Constants.REDIS_KEY_CHECK_CODE + registerReqVO.getCheckCodeKey());
        }
    }

    @RequestMapping("/login")
    public ResponseVO login(LoginReqVO loginReqVO) {
        try {
            log.info("登录请求的Redis Key：{}", Constants.REDIS_KEY_CHECK_CODE + loginReqVO.getCheckCodeKey());
            String redisCheckCode = (String) redisUtil.get(Constants.REDIS_KEY_CHECK_CODE + loginReqVO.getCheckCodeKey());
            if (null != redisCheckCode && !loginReqVO.getCheckCode().equalsIgnoreCase(redisCheckCode)) {
                throw new BusinessException("验证码不正确");
            }
            if (null == redisCheckCode) {
                throw new BusinessException("验证码已过期");
            }
            LoginRspVO loginRspVO = userInfoService.login(loginReqVO.getEmail(), loginReqVO.getPassword());
            return getSuccessResponseVO(loginRspVO);
        } finally {
            redisUtil.del(Constants.REDIS_KEY_CHECK_CODE + loginReqVO.getCheckCodeKey());
        }
    }

    @GlobalInterceptor
    @RequestMapping("/getSysSetting")
    public ResponseVO getSysSetting() {
        return getSuccessResponseVO(redisUtil.getSysSetting());
    }

}
